package jjn.round1;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author Jiang Jining
 * @since 2022-09-27 17:07
 */
public class Interview_1_2_CheckPermutationLcci {
    public boolean CheckPermutation(String s1, String s2) {
        if (s1 == null && s2 == null) {
            return true;
        }
        if (s1 == null || s2 == null) {
            return false;
        }
        Map<Character, Integer> map = new HashMap<>();
        for (char c : s1.toCharArray()) {
            Integer count = map.getOrDefault(c, 0);
            map.put(c, ++count);
        }
        Map<Character, Integer> target = new HashMap<>();
        for (char c : s2.toCharArray()) {
            if (!map.containsKey(c)) {
                return false;
            }
            Integer count = target.getOrDefault(c, 0);
            target.put(c, ++count);
        }
        Set<Character> characters = map.keySet();
        for (Character c : characters) {
            if (!Objects.equals(map.get(c), target.get(c))) {
                return false;
            }
        }
        return true;
    }
}
